<?php

namespace api\modules\Wechat\controllers;

use Yii;
use yii\helpers\Json;
use yii\web\Controller;

/**
 * Default controller for the `wechat` module
 */
class DefaultController extends \common\rest\controllers\Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    public function actionConfig($url, $debug = false)
    {
        try {
            $wjs = Yii::$app->wechat->app->js;
            $signPackage = $wjs->signature($url);
            $config = array_merge(
                ['debug' => $debug],
                $signPackage,
                [
                    'jsApiList' => [
                        'scanQRCode',
                        'updateTimelineShareData',
                        'updateAppMessageShareData',
                        'checkJsApi',
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage',
                        'onMenuShareQQ',
                        'onMenuShareWeibo',
                        'onMenuShareQZone',
                        'hideMenuItems',
                        'showMenuItems',
                        'hideAllNonBaseMenuItem',
                        'showAllNonBaseMenuItem',
                        'translateVoice',
                        'startRecord',
                        'stopRecord',
                        'onVoiceRecordEnd',
                        'playVoice',
                        'onVoicePlayEnd',
                        'pauseVoice',
                        'stopVoice',
                        'uploadVoice',
                        'downloadVoice',
                        'chooseImage',
                        'previewImage',
                        'uploadImage',
                        'downloadImage',
                        'getNetworkType',
                        'openLocation',
                        'getLocation',
                        'hideOptionMenu',
                        'showOptionMenu',
                        'closeWindow',
                        'scanQRCode',
                        'chooseWXPay',
                        'openProductSpecificView',
                        'addCard',
                        'chooseCard',
                        'openCard',
                    ]
                ]
            );

            return $config;

            // $js = "";
            // $js .= "wx.config(" . $wjs->config([
            //         'scanQRCode',
            //         'checkJsApi',
            //         'onMenuShareTimeline',
            //         'onMenuShareAppMessage',
            //         'onMenuShareQQ',
            //         'onMenuShareWeibo',
            //         'onMenuShareQZone',
            //         'hideMenuItems',
            //         'showMenuItems',
            //         'hideAllNonBaseMenuItem',
            //         'showAllNonBaseMenuItem',
            //         'translateVoice',
            //         'startRecord',
            //         'stopRecord',
            //         'onVoiceRecordEnd',
            //         'playVoice',
            //         'onVoicePlayEnd',
            //         'pauseVoice',
            //         'stopVoice',
            //         'uploadVoice',
            //         'downloadVoice',
            //         'chooseImage',
            //         'previewImage',
            //         'uploadImage',
            //         'downloadImage',
            //         'getNetworkType',
            //         'openLocation',
            //         'getLocation',
            //         'hideOptionMenu',
            //         'showOptionMenu',
            //         'closeWindow',
            //         'scanQRCode',
            //         'chooseWXPay',
            //         'openProductSpecificView',
            //         'addCard',
            //         'chooseCard',
            //         'openCard',], false) . ")";
            // return $js;
        } catch (\Exception $exception) {
            return $exception->getMessage();
        }

    }
}
